package com.zhihua.interview.merge.biz;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

@Service
public class MergeBiz {

    public String mergeNumberBiz(String test) {
        JSONObject jsonObject1 = JSONObject.parseObject(test);
        JSONArray newstock = new JSONArray();
        for (int i = 0; i < jsonObject1.size(); i++) {
            JSONArray stock = (JSONArray) jsonObject1.get("stock");
            JSONArray order = (JSONArray) jsonObject1.get("order");
            JSONArray objects = new JSONArray();
            ArrayList arrayList = new ArrayList();
            for (int j = 0; j < order.size(); j++) {
                JSONObject orders = (JSONObject) order.get(j);
                if (objects.size() == 0) {
                    objects.add(orders);
                    arrayList.add(orders.get("goodsId"));
                } else {
                    if (arrayList.contains(orders.get("goodsId"))) {
                        z:
                        for (int k = 0; k < objects.size(); k++) {
                            JSONObject json = (JSONObject) objects.get(k);
                            if (json.get("goodsId").equals(orders.get("goodsId"))) {
                                JSONObject jsonObject = new JSONObject();
                                jsonObject.put("goodsId", orders.get("goodsId"));
                                jsonObject.put("name", orders.get("name"));
                                jsonObject.put("number", (Integer) orders.get("number") + (Integer) json.get("number"));
                                objects.remove(k);
                                objects.add(jsonObject);
                                break z;
                            }
                        }
                    } else {
                        objects.add(orders);
                        arrayList.add(orders.get("goodsId"));
                    }
                }
            }
            for (int j = 0; j < stock.size(); j++) {
                JSONObject stocks = (JSONObject) stock.get(j);
                if (arrayList.contains(stocks.get("goodsId"))) {
                    f:
                    for (int k = 0; k < objects.size(); k++) {
                        JSONObject json = (JSONObject) objects.get(k);
                        if (json.get("goodsId").equals(stocks.get("goodsId"))) {
                            JSONObject jsonObject = new JSONObject();
                            jsonObject.put("goodsId", stocks.get("goodsId"));
                            jsonObject.put("name", stocks.get("name"));
                            jsonObject.put("number", (Integer) stocks.get("number") + (Integer) json.get("number"));
                            newstock.add(jsonObject);
                            break f;
                        }
                    }
                }
            }
            break;
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("stock", newstock);
        return jsonObject.toString();
    }
}
